package cn.momoshenchi.service.impl;

import cn.momoshenchi.mapper.MatchOptionMapper;
import cn.momoshenchi.mapper.SearchMapper;
import cn.momoshenchi.pojo.Article;
import cn.momoshenchi.pojo.Book;
import cn.momoshenchi.pojo.MatchOption;
import cn.momoshenchi.service.ArticleService;
import cn.momoshenchi.service.BookService;
import cn.momoshenchi.service.MatchOptionService;
import cn.momoshenchi.service.SearchService;
import cn.momoshenchi.vo.SearchVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/6/21 - 8:21
 */
@Service
public class SearchServiceImpl extends ServiceImpl<SearchMapper, SearchVo> implements SearchService
{
    @Autowired
    private BookService bookService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public  SearchVo searchArticleAndBook (String keyword) throws IOException, InvocationTargetException, IllegalAccessException
    {
        List<Book> book = bookService.search(keyword);
        List<Article> article = articleService.search(keyword);
        SearchVo searchVo = new SearchVo();
        searchVo.setBookList(book);
        searchVo.setArticleList(article);


//        SearchRequest searchRequest=new SearchRequest("book");
//        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
//        searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("title",keyword));
////        searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("description",keyword));
//        searchRequest.source(searchSourceBuilder);
//        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT.toBuilder().build());
//
//        SearchHit[] hits = search.getHits().getHits();
//        System.out.println(Arrays.toString(hits));
//        List<Book>bookList=new ArrayList<>();
//        for (SearchHit hit : hits)
//        {
//            Map<String, Object> map = hit.getSourceAsMap();
//            Book book = new Book();
//            book.setId(Long.valueOf((Integer) map.get("id")));
//            book.setTitle((String) map.get("title"));
//            book.setDescription((String) map.get("description"));
//            book.setImg((String) map.get("img"));
//            book.setLevel((Integer) map.get("level"));
//            bookList.add(book);
//        }
//    searchVo.setBookList(bookList);
//
//         searchRequest=new SearchRequest("article");
//         searchSourceBuilder=new SearchSourceBuilder();
//        searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("content",keyword));
////        searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("title",keyword));
//        searchRequest.source(searchSourceBuilder);
//         search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT.toBuilder().build());
//
//         hits = search.getHits().getHits();
//        System.out.println(Arrays.toString(hits));
//        List<Article>articleList=new ArrayList<>();
//        for (SearchHit hit : hits)
//        {
//            Map<String, Object> map = hit.getSourceAsMap();
//            Article article = new Article();
//            article.setId(Long.valueOf((Integer) map.get("id")));
//            article.setTitle((String) map.get("title"));
//            article.setContent((String) map.get("content"));
//            article.setNickname((String) map.get("nickname"));
//            article.setPhotoa((String) map.get("photoa"));
//            articleList.add(article);
//        }
//        searchVo.setArticleList(articleList);
        return  searchVo;
    }

}
